package com.jfree.nettyshow.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelPromise;
import io.netty.handler.ssl.OptionalSslHandler;

import java.net.SocketAddress;
import java.util.WeakHashMap;

// todo 里面方法时候被触发调用？？？
public class OutHand implements ChannelOutboundHandler {
    @Override
    public void bind(ChannelHandlerContext ctx, SocketAddress localAddress, ChannelPromise promise) throws Exception {
        System.out.println("ChannelOutboundHandler---bind---");
        ctx.bind(localAddress, promise);
    }

    @Override
    public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) throws Exception {
        System.out.println("ChannelOutboundHandler---connect---");
        ctx.connect(remoteAddress, localAddress, promise);
    }

    @Override
    public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
        System.out.println("ChannelOutboundHandler---disconnect---");
        ctx.disconnect(promise);
    }

    @Override
    public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
        System.out.println("ChannelOutboundHandler---close---");
        ctx.close(promise);
    }

    @Override
    public void deregister(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
        System.out.println("ChannelOutboundHandler---deregister---");
        ctx.deregister(promise);
    }

    @Override
    public void read(ChannelHandlerContext ctx) throws Exception {
        ctx.read(); //todo 这里如果注释掉则，读取服务端不读客户端的数据了
        System.out.println("ChannelOutboundHandler---read---");
        System.out.println("--out--ctx.channel().isActive()="+ctx.channel().isActive());
        System.out.println("----out--ctx.channel().isRegistered()"+ctx.channel().isRegistered());
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        ctx.write(msg);
        System.out.println("ChannelOutboundHandler---write---");
    }

    @Override
    public void flush(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
        System.out.println("ChannelOutboundHandler---flush---");
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        //OptionalSslHandler
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

    }

    public static void main(String[] args) {
        assert 1 > 12 : "11111111111111";
        System.out.println("1--2");

    }

}
